﻿Imports Excepciones
Imports Negocio
Imports System.Globalization
Imports System.ComponentModel

Public Class DatePickerExt
    Inherits MaskedTextBox
    Implements IControlValidatable

    Sub New()
        Me.Size = New System.Drawing.Size(80, 22)
        Me.Mask = "00/00/0000"
    End Sub

    Public Property Value() As System.Nullable(Of Date)
        Get
            If IsDate(Me.Text) Then
                Return Convert.ToDateTime(Me.Text)
            Else
                Return Nothing
            End If
        End Get
        Set(ByVal value As System.Nullable(Of Date))
            If value Is Nothing Then
                Me.Text = ""
            Else
                Try
                    Me.Text = Format(value, "dd/MM/yyyy")
                Catch ex As Exception
                    Me.Text = ""
                End Try
            End If
        End Set
    End Property

#Region "Validacion"

    ''' <summary>
    ''' Se produce cuando el control de esta validando, pero antes de la validación por tipo
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event ValidatingBefore(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

    ''' <summary>
    ''' Se produce cuando el control de esta validando, pero luego de la validación por tipo
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event ValidatingAfter(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

    Private _validations As New List(Of Utilidades.ValidationType)
    <Browsable(True), _
    EditorBrowsable(EditorBrowsableState.Always), _
    Category("Validation"), _
    Description("Valida el texto con las opciones seleccionadas"), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public Property Validation() As List(Of Utilidades.ValidationType)
        Get
            Return _validations
        End Get
        Set(ByVal value As List(Of Utilidades.ValidationType))
            _validations = value
        End Set
    End Property

    Public Sub ValidateInput() Implements IControlValidatable.ValidateInput
        ValidatingDate()
        Dim text As String = Me.Text
        If text = "  /  /" Then
            text = String.Empty
        End If
        For Each tipo As Utilidades.ValidationType In _validations
            Utilidades.Validations.Validate(tipo, text)
        Next
    End Sub

    Protected Overridable Sub Me_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Validating
        Dim frm As Base
        Try
            frm = DirectCast(Me.FindForm(), Base)
            frm.ResetErrorUI(sender)
            Try
                RaiseEvent ValidatingBefore(sender, e)
                ValidateInput()
                RaiseEvent ValidatingAfter(sender, e)
            Catch ex As CustomException
                e.Cancel = True
                frm.SetErrorUI(sender, ex)
            End Try
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub ValidatingDate()
        'Verifica que si no tiene la validacion de fecha, la agrega
        Dim validDate As Boolean = False
        For Each tipo As Utilidades.ValidationType In _validations
            If tipo = Utilidades.ValidationType.Date Then
                validDate = True
                Exit For
            End If
        Next
        If Not validDate Then
            Me.Validation.Add(Utilidades.ValidationType.Date)
        End If
    End Sub
#End Region

End Class
